package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.dto.BuyListDTO;
import net.wanho.service.BuyListService;
import net.wanho.vo.AjaxResult;
import net.wanho.vo.BuyListVo;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/purchase")
@RequiredArgsConstructor
public class PurchaseController {
    private final BuyListService buyListService;

    /**
     * 采购单分页列表
     *
     * @param buyListDTO
     * @param pageNum
     * @param pageSize
     */
    @GetMapping("/purchase-page-list")
    public AjaxResult purchasePageList(BuyListDTO buyListDTO,
                                       @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                       @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return AjaxResult.success(buyListService.getListPage(buyListDTO, pageNum, pageSize));
    }

    /**
     * 采购单添加
     * @param buyListVo
     */
    @PostMapping("/purchase-add")
    public AjaxResult purchaseAdd(@RequestBody BuyListVo buyListVo) {
        buyListService.insertBuyList(buyListVo);
        return AjaxResult.success(null, "采购单添加成功！");
    }

    /**
     * 修改采购单
     */
    @PutMapping("/purchase-update")
    public AjaxResult purchaseUpdate(@RequestBody BuyListVo buyListVo) {
        buyListService.updatePurchaseById(buyListVo);
        return AjaxResult.success(null, "采购单修改成功！");
    }

    /**
     * 删除采购单
     */
    @DeleteMapping("/purchase-delete/{id}")
    public AjaxResult purchaseDelete(@PathVariable Integer id) {
        boolean isDelete = buyListService.removeById(id);
        if (!isDelete) {
            return AjaxResult.error("采购单删除失败！");
        }
        return AjaxResult.success(null, "采购单删除成功！");
    }

    /**
     * 生成入库单
     */
    @PostMapping("/in-warehouse-record-add")
    public AjaxResult inWarehouseRecordAdd(@RequestBody BuyListVo buyListVo) {
        buyListVo.setIsIn("1");
        buyListService.updatePurchaseById(buyListVo);
        return AjaxResult.success(null, "入库单生成成功！");
    }
    /**
     * 采购单导出
     */
    @GetMapping("/exportTable")
    public AjaxResult exportTable(BuyListDTO buyListDTO,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return AjaxResult.success(buyListService.getListPage(buyListDTO, pageNum, pageSize).getResultList());
    }
}
